/*
    Copyright 2010 Northbranchlogic, Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.norbl.runjrun.gui;

import java.awt.event.*;
import java.awt.*;
import java.io.*;
import com.norbl.util.*;

/**
 *
 * @author Barnet Wagman
 */
public class ParamDialog extends javax.swing.JDialog
    implements ActionListener {

    static String OK = "Ok";
    static String CANCEL = "Cancel";
    static String CHANGE_DIR = "ChangeDir";

    public File paramFile;
    public String awsAccessID;
    public String awsSecretKey;
    public String bucketName;

    public boolean paramsChanged;
    public boolean saveParams;

    /** Creates new form ParamDialog */
    public ParamDialog(java.awt.Frame parent, boolean modal,
                       File paramFile,
                       String awsAccessID,
                       String awsSecretKey,
                       String bucketName) {
        super(parent, modal);
        this.paramFile = paramFile;
        this.awsAccessID = awsAccessID;
        this.awsSecretKey = awsSecretKey;
        this.bucketName = bucketName;
        paramsChanged = false;
        saveParams = false;        
    }

    public void run() {       
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        paramsChanged = false;
                        saveParams = false;
                        dispose();
                    }
                });
                initComponents();
                GuiUtil.centerOnScreen(ParamDialog.this);
                setVisible(true);
            }
        });
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ev) {

        String c = ev.getActionCommand();
        if ( c.equals(CANCEL) ) {
            paramsChanged = false;
            saveParams = false;
            this.dispose();
            return;
        }
        else if ( c.equals(OK) ) {
            awsAccessID = awsAccessIDField.getText();
            awsSecretKey = awsSecretKeyField.getText();
            bucketName = bucketNameField.getText();
            paramsChanged = true;
            saveParams = saveParamsCheckBox.isSelected();
            this.dispose();
            return;
        }
        else if ( c.equals(CHANGE_DIR) ) {
            File dir = GuiUtil.getDir("Choose directory for param file",
                                       paramFile.getParentFile());
            if ( dir != null ) {
                paramFile = new File(dir,paramFile.getName());
                paramFileLabel.setText(paramFile.getPath());
            }
        }
    }

//    File getDefaultDir() {
//        File f = new File(System.getProperty("user.home") );
//        if ( f.exists() && f.isDirectory() ) return(f);
//        else return(null);
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        awsAccessIDFieldLabel = new javax.swing.JLabel();
        awsSecretKeyFieldLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        bucketNameField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        saveParamsCheckBox = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        chooseDirButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        paramFileLabel = new javax.swing.JLabel();
        awsAccessIDField = new javax.swing.JTextField();
        awsSecretKeyField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

        awsAccessIDFieldLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        awsAccessIDFieldLabel.setText("AWS Access ID");

        awsSecretKeyFieldLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        awsSecretKeyFieldLabel.setText("AWS Secret Key");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        jLabel3.setText("Bucketname");

        bucketNameField.setText(bucketName);
        bucketNameField.setFont(new java.awt.Font("DejaVu Sans", 0, 12));

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 0, 16)); // NOI18N
        jLabel1.setText("Enter Parameters");
        jLabel1.setToolTipText("These three parameters are required; the RunJRun monitor cannot function without them.");

        saveParamsCheckBox.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        saveParamsCheckBox.setText("Save parameters");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        jLabel2.setText("Parameter file:");

        chooseDirButton.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        chooseDirButton.setText("Choose a different directory for the parameter file");
        chooseDirButton.setActionCommand(CHANGE_DIR);
        chooseDirButton.addActionListener(this);

        okButton.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        okButton.setText("Ok");
        okButton.setActionCommand(OK);
        okButton.addActionListener(this);

        cancelButton.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        cancelButton.setText("Cancel");
        cancelButton.setActionCommand(CANCEL);
        cancelButton.addActionListener(this);

        paramFileLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        paramFileLabel.setText("jLabel4");
        String pfp = "";
        if ( paramFile != null ) pfp = paramFile.getPath();
        paramFileLabel.setText(pfp);

        awsAccessIDField.setText(awsAccessID);

        awsSecretKeyField.setText(awsSecretKey);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(awsAccessIDFieldLabel)
                            .addComponent(awsSecretKeyFieldLabel)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bucketNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(paramFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(chooseDirButton)
                            .addComponent(awsAccessIDField, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(awsSecretKeyField, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 465, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(saveParamsCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(awsAccessIDFieldLabel)
                    .addComponent(awsAccessIDField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(awsSecretKeyFieldLabel)
                    .addComponent(awsSecretKeyField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bucketNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(48, 48, 48)
                .addComponent(saveParamsCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(paramFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chooseDirButton)
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        ParamDialog dialog = new ParamDialog(new javax.swing.JFrame(),
                                            true,new File("/x/y/z.txt"),
                                            "ID","Key","bucket");
        dialog.run();
//        dialog.setVisible(true);
        System.out.println("BACK from run:\n" +
                dialog.paramsChanged + " " + dialog.saveParams + "\n" +
                dialog.awsAccessID + " " + dialog.awsSecretKey + "\n" +
                dialog.bucketName + "\n" +
                dialog.paramFile.getPath());

        System.exit(0);

//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                ParamDialog dialog = new ParamDialog(new javax.swing.JFrame(),
//                                            true,new File("/x/y/z.txt"),
//                                            "ID","Key","bucket");
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        // set boos = false
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField awsAccessIDField;
    private javax.swing.JLabel awsAccessIDFieldLabel;
    private javax.swing.JTextField awsSecretKeyField;
    private javax.swing.JLabel awsSecretKeyFieldLabel;
    private javax.swing.JTextField bucketNameField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseDirButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel paramFileLabel;
    private javax.swing.JCheckBox saveParamsCheckBox;
    // End of variables declaration//GEN-END:variables

}
